# -*- coding: utf-8 -*-
from lxml import etree
import requests
from config import proxy

# 代理 IP
_proxy = requests.get(proxy).text
proxies = {
  'http': 'http://' + _proxy
}

def get_img_url(obj):

  # url = 'https://itemcdn.tmall.com/1688offer/icoss2216880252cb747993ec73eb32'

  Header = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'no-cache',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'none',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
  }
  
  res = requests.get(obj.url,headers=Header,proxies=proxies)

  html = etree.HTML(res.text)
  urlStr = html.xpath("//img[@alt='\\\"undefined\\\"']/@src")
  obj.imgUrls = []
  for url in urlStr:
    obj.imgUrls.append(url[2:-2])
  print("解析一页数据成功！")